home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Snippets / Platforms & Tools / MacApp / MouseInfo 1.0 / UMenuedWindow.cp < prev    next >
Encoding:
Text File  |  1992-04-29  |  10.5 KB  |  387 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    UMenuedWindow.cp
  3.  *
  4.  *    Copyright 1991 Imagenetics
  5.  *    All rights reserved
  6.  *
  7.  *    19 Aug 91  EMB  Created file
  8.  *    22 Aug 91  EMB  Modified design to be standalone unit
  9.  *     1 Oct 91  LPG  Changed Str255 to CStr255 (MacApp 3.0ß2)
  10.  *                    Catch empty strings in AddToWindowMenu
  11.  *
  12.  */
  13.  
  14. #ifndef        __UMENUEDWINDOW__
  15. #include    "UMenuedWindow.h"
  16. #endif
  17.  
  18. class TMenuedWindowBehavior : public TBehavior
  19. {
  20. private:
  21.     TList*    fWindowsMenuData;
  22.  
  23.     short    fMenuID;
  24.     short    fFirstItem;
  25. public:
  26.  
  27.     // Construction/Destruction
  28.     virtual pascal void    Free();                                        //    OVERRIDE
  29.     virtual pascal void    IMenuedWindowBehavior(short menuID);
  30.     virtual pascal void    Initialize();                                //    OVERRIDE
  31.  
  32.     // Menu Commands
  33.     virtual pascal void    DoMenuCommand(CommandNumber aCommand);        //    OVERRIDE
  34.     virtual pascal void    DoSetupMenus();                                //    OVERRIDE
  35.  
  36.     // Window menu
  37.     virtual pascal void        AddToWindowMenu(TWindow* aWindow);
  38.     virtual pascal void        DeleteFromWindowMenu(TWindow* aWindow);
  39.     virtual pascal TWindow*    MenuItemToWindow(short menuItem);
  40.     virtual pascal void        SelectWindowMenuWindow(short itemNo);
  41.     virtual pascal short    WindowToMenuItem(TWindow* aWindow);
  42. };
  43.  
  44.  
  45. /*********************************************************************************************
  46.     U M e n u e d W i n d o w    G l o b a l    R o u t i n e s
  47. *********************************************************************************************/
  48.  
  49. TMenuedWindowBehavior*    gMenuedWindowBehavior = NULL;
  50.  
  51. //--------------------------------------------------------------------------------------------------
  52. #pragma segment AInit
  53. pascal void InitUMenuedWindow(ResNumber menuID)
  54. {
  55.     gMenuedWindowBehavior = NULL;
  56.  
  57.     if (gDeadStripSuppression)
  58.     {
  59.         macroDontDeadStrip(TFloatMenuedWindow);
  60.         macroDontDeadStrip(TMenuedWindow);
  61.     }
  62.  
  63.     if (gApplication == NULL)
  64.     {
  65.         if (qDebug)
  66.             ProgramBreak("InitUMenuedWindow must be called in or after your IMyApplication method");
  67.         Failure(minErr, 0);
  68.     }
  69.  
  70.     gMenuedWindowBehavior = new TMenuedWindowBehavior;
  71.     gMenuedWindowBehavior->IMenuedWindowBehavior(menuID);
  72.     gApplication->AddBehavior(gMenuedWindowBehavior);
  73. }
  74.  
  75. //--------------------------------------------------------------------------------------------------
  76. /*
  77.  *    In the spirit of SetCommandName and SetCommandIcon we have another utility
  78.  *    routine to handle the menu item style
  79.  */
  80. #pragma segment MAUtilitiesRes
  81. pascal void SetCommandStyle(CommandNumber aCommand, short chStyle)
  82. {
  83.     ResNumber    menu;
  84.     short        item;
  85.     MenuHandle    aMenuHandle;
  86.  
  87.     aMenuHandle = CommandToComponents(aCommand, menu, item);
  88.     if (aMenuHandle)
  89.         SetItemStyle(aMenuHandle, item, chStyle);
  90. }
  91.  
  92.  
  93. /*********************************************************************************************
  94.     T M e n u e d W i n d o w C o H a n d l e r
  95. *********************************************************************************************/
  96.  
  97. //--------------------------------------------------------------------------------------------------
  98. #pragma segment ARes
  99. pascal void TMenuedWindowBehavior::AddToWindowMenu(TWindow* aWindow)
  100. {
  101.     CStr255        itsTitle;
  102.  
  103.     aWindow->GetTitle(itsTitle);
  104.  
  105.     if (itsTitle.IsEmpty())
  106.         itsTitle = " ";        // AppendMenu doesn't like empty strings.
  107.  
  108.     AppendMenu(MAGetMenu(fMenuID), itsTitle);
  109.  
  110.     fWindowsMenuData->InsertLast(aWindow);
  111. }
  112.  
  113.  
  114. //--------------------------------------------------------------------------------------------------
  115. #pragma segment ASelCommand
  116. pascal void TMenuedWindowBehavior::DoMenuCommand(CommandNumber aCommand)        // OVERRIDE
  117. {
  118.     short    itsMenuID;
  119.     short    itsMenuItem;
  120.  
  121.     if (aCommand < 0)
  122.     {
  123.         CommandToMenuItem(aCommand, itsMenuID, itsMenuItem);
  124.         if (itsMenuID == fMenuID)
  125.             this->SelectWindowMenuWindow(itsMenuItem);
  126.         else
  127.             inherited::DoMenuCommand(aCommand);
  128.     }
  129.     else
  130.         inherited::DoMenuCommand(aCommand);
  131. }
  132.  
  133.  
  134. //--------------------------------------------------------------------------------------------------
  135. #pragma segment ARes
  136. pascal void TMenuedWindowBehavior::DoSetupMenus()        // OVERRIDE
  137. {
  138.     CommandNumber    itsCommandNumber;
  139.     TWindow*        itsWindow = NULL;
  140.     short            frontMenuItem;
  141.     CStr255            itsTitle;
  142.     short            itsMenuItem;
  143.     short            i;
  144.  
  145.     inherited::DoSetupMenus();
  146.  
  147.     //    Enable the variant portion of the windows menu
  148.     frontMenuItem = this->WindowToMenuItem(gApplication->GetActiveWindow());
  149.     for (i = short(fWindowsMenuData->GetSize()); i > 0; i--)
  150.     {
  151.         itsMenuItem = i + (fFirstItem - 1);
  152.         itsCommandNumber = CommandFromMenuItem(fMenuID, itsMenuItem);
  153.         itsWindow = this->MenuItemToWindow(itsMenuItem);
  154.  
  155.         //    Enable the item, checking if it is the front item
  156.         EnableCheck(itsCommandNumber, TRUE, itsMenuItem == frontMenuItem);
  157.  
  158.         //    Make sure the name is up-to-date
  159.         itsWindow->GetTitle(itsTitle);
  160.         SetCommandName(itsCommandNumber, itsTitle);
  161.  
  162.         //    Make the item style plain if it is visible, italics if hidden
  163.         if (itsWindow->IsShown())
  164.             SetCommandStyle(itsCommandNumber, normal);
  165.         else
  166.             SetCommandStyle(itsCommandNumber, italic);
  167.  
  168.         itsWindow = NULL;
  169.     }
  170. }
  171.  
  172. //--------------------------------------------------------------------------------------------------
  173. #pragma segment ARes
  174. pascal void TMenuedWindowBehavior::DeleteFromWindowMenu(TWindow* aWindow)
  175. {
  176.     short    itsMenuItem;
  177.  
  178.     if (aWindow != NULL)
  179.     {
  180.         itsMenuItem = this->WindowToMenuItem(aWindow);
  181.  
  182.         if (itsMenuItem != kEmptyIndex)
  183.             DelMenuItem(MAGetMenu(fMenuID), itsMenuItem);
  184.  
  185.         fWindowsMenuData->Delete(aWindow);
  186.     }
  187. }
  188.  
  189. //--------------------------------------------------------------------------------------------------
  190. #pragma segment ATerminate
  191. pascal void TMenuedWindowBehavior::Free()                        // OVERRIDE
  192. {
  193.     fWindowsMenuData = (TList*)FreeIfObject(fWindowsMenuData);
  194.  
  195.     inherited::Free();
  196. }
  197.  
  198. //--------------------------------------------------------------------------------------------------
  199. #pragma segment AInit
  200. pascal void TMenuedWindowBehavior::IMenuedWindowBehavior(short menuID)
  201. {
  202.     FailInfo    fi;
  203.  
  204.     this->IBehavior(kMenuedWindowBehavior);
  205.  
  206.     if (fi.Try())
  207.     {
  208.         fWindowsMenuData = NewList();
  209.         if (qDebug)
  210.             fWindowsMenuData->SetEltType("TWindow");
  211.  
  212.         fMenuID = menuID;
  213.         fFirstItem = CountMItems(MAGetMenu(menuID)) + 1;
  214.  
  215.         fi.Success();
  216.     }
  217.     else
  218.     {
  219.         this->Free();
  220.         fi.ReSignal();
  221.     }
  222. }
  223.  
  224. //--------------------------------------------------------------------------------------------------
  225. #pragma segment AInit
  226. pascal void TMenuedWindowBehavior::Initialize()                    // OVERRIDE
  227. {
  228.     inherited::Initialize();
  229.  
  230.     fWindowsMenuData = NULL;
  231.  
  232.     fMenuID = 0;
  233.     fFirstItem = 0;
  234. }
  235.  
  236. //--------------------------------------------------------------------------------------------------
  237. #pragma segment ARes
  238. pascal TWindow* TMenuedWindowBehavior::MenuItemToWindow(short menuItem)
  239. {
  240.     TWindow*    result = NULL;
  241.     ArrayIndex    windowIndex;
  242.  
  243.     if (menuItem > 0)
  244.     {
  245.         windowIndex = menuItem - (fFirstItem - 1);
  246.         if (windowIndex > kEmptyIndex  &&  windowIndex <= fWindowsMenuData->GetSize())
  247.             result = (TWindow*)fWindowsMenuData->At(windowIndex);
  248.     }
  249.  
  250.     return result;
  251. }
  252.  
  253. //--------------------------------------------------------------------------------------------------
  254. #pragma segment ARes
  255. pascal void TMenuedWindowBehavior::SelectWindowMenuWindow(short itemNo)
  256. {
  257.     TWindow*    itsWindow;
  258.  
  259.     if (itemNo > 0)
  260.     {
  261.         itsWindow = this->MenuItemToWindow(itemNo);
  262.         if (itsWindow != NULL)
  263.         {
  264.             if (!itsWindow->IsShown())
  265.                 itsWindow->Open();
  266.             itsWindow->Select();
  267.         }
  268.         else if (qDebug)
  269.             ProgramBreak("Problem: I’ve been asked to select a Windows menu window that doesn’t exist");
  270.     }
  271. }
  272.  
  273. //--------------------------------------------------------------------------------------------------
  274. #pragma segment ARes
  275. pascal short TMenuedWindowBehavior::WindowToMenuItem(TWindow* aWindow)
  276. {
  277.     short    result = 0;
  278.     short    windowIndex;
  279.  
  280.     if (aWindow != NULL)
  281.     {
  282.         //    The fourth item is the first one placed in the windows menu (and hence
  283.         //    tracked in fWindowsMenuData.
  284.         windowIndex = short(fWindowsMenuData->GetIdentityItemNo(aWindow));
  285.         if (windowIndex != kEmptyIndex)
  286.             result = windowIndex + (fFirstItem - 1);
  287.     }
  288.  
  289.     return result;
  290. }
  291.  
  292. /*********************************************************************************************
  293.     T F l o a t M e n u e d W i n d o w
  294. *********************************************************************************************/
  295. //--------------------------------------------------------------------------------------------------
  296. #pragma segment AOpen
  297. pascal void TFloatMenuedWindow::DoPostCreate(TDocument* itsDocument)        // OVERRIDE
  298. {
  299.     inherited::DoPostCreate(itsDocument);
  300.  
  301.     if (gMenuedWindowBehavior != NULL)
  302.         gMenuedWindowBehavior->AddToWindowMenu(this);
  303. }
  304.  
  305. //--------------------------------------------------------------------------------------------------
  306. #pragma segment AClose
  307. pascal void TFloatMenuedWindow::Free()                                        // OVERRIDE
  308. {
  309.     if (gMenuedWindowBehavior != NULL)
  310.         gMenuedWindowBehavior->DeleteFromWindowMenu(this);
  311.  
  312.     inherited::Free();
  313. }
  314.  
  315. //--------------------------------------------------------------------------------------------------
  316. #pragma segment AOpen
  317. pascal void TFloatMenuedWindow::IFloatMenuedWindow(TDocument* itsDocument,
  318.                                        WindowPtr itsWMgrWindow, Boolean canResize,
  319.                                        Boolean canClose, Boolean disposeOnFree)
  320. {
  321.  
  322.     this->IFloatWindow(itsDocument, itsWMgrWindow, canResize, canClose, disposeOnFree);
  323.  
  324.     FailInfo    fi;
  325.     if (fi.Try())
  326.     {
  327.         if (gMenuedWindowBehavior != NULL)
  328.             gMenuedWindowBehavior->AddToWindowMenu(this);
  329.  
  330.         fi.Success();
  331.     }
  332.     else
  333.     {
  334.         this->Free();
  335.         fi.ReSignal();
  336.     }
  337. }
  338.  
  339. /*********************************************************************************************
  340.     T M e n u e d W i n d o w
  341. *********************************************************************************************/
  342. //--------------------------------------------------------------------------------------------------
  343. #pragma segment AOpen
  344. pascal void TMenuedWindow::DoPostCreate(TDocument* itsDocument)            // OVERRIDE
  345. {
  346.     inherited::DoPostCreate(itsDocument);
  347.  
  348.     if (gMenuedWindowBehavior != NULL)
  349.         gMenuedWindowBehavior->AddToWindowMenu(this);
  350. }
  351.  
  352.  
  353. //--------------------------------------------------------------------------------------------------
  354. #pragma segment AClose
  355. pascal void TMenuedWindow::Free()                                        // OVERRIDE
  356. {
  357.     if (gMenuedWindowBehavior != NULL)
  358.         gMenuedWindowBehavior->DeleteFromWindowMenu(this);
  359.  
  360.     inherited::Free();
  361. }
  362.  
  363. //--------------------------------------------------------------------------------------------------
  364. #pragma segment AOpen
  365. pascal void TMenuedWindow::IMenuedWindow(TDocument* itsDocument, WindowPtr itsWMgrWindow,
  366.                              Boolean canResize, Boolean canClose,
  367.                              Boolean disposeOnFree)
  368. {
  369.  
  370.     this->IWindow(itsDocument, itsWMgrWindow, canResize, canClose, disposeOnFree);
  371.  
  372.     FailInfo    fi;
  373.     if (fi.Try())
  374.     {
  375.         if (gMenuedWindowBehavior != NULL)
  376.             gMenuedWindowBehavior->AddToWindowMenu(this);
  377.  
  378.         fi.Success();
  379.     }
  380.     else
  381.     {
  382.         this->Free();
  383.         fi.ReSignal();
  384.     }
  385. }
  386.  
  387.